<template @update-cur-state-officer="test">
  <a-descriptions title="当前状态">
    <a-descriptions-item label="已借出麦克风数量">
      {{ curState.out }}
    </a-descriptions-item>
    <a-descriptions-item label="剩余麦克风">
      {{ curState.remain }}
    </a-descriptions-item>
    <a-descriptions-item label="麦克风上限">
      {{ curState.max }}
    </a-descriptions-item>
  </a-descriptions>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import { BASE_URL, myPost, Response } from "@/utils/request";
import { useStore } from "vuex";
import { message } from "ant-design-vue";

export default defineComponent({
  name: "CurState",
  setup(props, ctx) {
    const store = useStore();
    let respBody = {
      isSuccess: false,
      msg: "",
      data: []
    };
    const curState = reactive({
      out: 0,
      remain: 0,
      max: 0
    });

    function queryOfficerBorrow() {
      myPost(BASE_URL + "/officer/queryOfficerBorrow", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            curState.out = respBody.data.length;
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log("curState" + error);
          message.error({ content: error, duration: 2 });
        });
    }

    function queryRemain() {
      myPost(BASE_URL + "/officer/queryRemain", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            curState.remain = respBody.data[0];
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log("curState" + error);
          message.error({ content: error, duration: 2 });
        });
    }

    function queryMax() {
      myPost(BASE_URL + "/officer/queryMax", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            curState.max = respBody.data[0];
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log("curState" + error);
          message.error({ content: error, duration: 2 });
        });
    }

    function update() {
      console.log("EMIT TEST");
      queryOfficerBorrow();
      queryRemain();
      queryMax();
    }

    function test() {
      console.log("NMSL");
    }

    return {
      curState,
      update,
      test
    };
  }
});
</script>

<style scoped></style>
